import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import PIL.Image as Image

image = Image.open(r'D:\AITest\pro_python\test-w10.jpg')
image_array = np.array(image)

# print(image_array.shape)
# print(image_array.dtype)
# (1440, 1080, 3)
# uint8

w_r = tf.placeholder(shape=[None, None], dtype=tf.float32)
w_g = tf.placeholder(shape=[None, None], dtype=tf.float32)
w_b = tf.placeholder(shape=[None, None], dtype=tf.float32)

w_rgb = tf.stack([w_r, w_g, w_b], axis=2)
# # print(w_rgb)
# Tensor("stack:0", shape=(?, ?, 3), dtype=float32)

# 增加一个维度
w_expand = tf.expand_dims(w_rgb, axis=-1)
# print(w_expand)
# Tensor("ExpandDims:0", shape=(?, ?, 3, 1), dtype=float32)

x = tf.placeholder(shape=[None, None, 3], dtype=tf.uint8)
inp = tf.to_float(x)
# print(inp)
# Tensor("ToFloat:0", shape=(?, ?, 3), dtype=float32)

inp = tf.expand_dims(inp, axis=0)
# print(inp)
# Tensor("ExpandDims_1:0", shape=(1, ?, ?, 3), dtype=float32)

output = tf.nn.conv2d(inp, w_expand, strides=[1, 1, 1, 1], padding='VALID')
w_r_value = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype=float)
w_g_value = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype=float)
w_b_value = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype=float)

sess = tf.Session()
output_value = sess.run(output, feed_dict={x:image_array,
                                           w_r:w_r_value,
                                           w_g:w_g_value,
                                           w_b:w_b_value})
# print(output_value.shape)
# (1, 1438, 1078, 1)

output_pic = np.squeeze(output_value)
# print(output_pic.shape)
# (1438, 1078)
# 显示原图
# plt.imshow(image)
# plt.show()
# # 显示灰度图
# plt.imshow(output_pic, cmap='gray')
# plt.show()

# w_r_value = np.array([[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]], dtype=float)
# w_g_value = np.array([[1,0,0,0,0], [0,1,0,0,0], [0,0,1,0,0], [0,0,0,1,0], [0,0,0,0,1]], dtype=float)
# w_b_value = np.array([[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]], dtype=float)
# w_value, output_value = sess.run([w_expand,output], feed_dict = {x:image_array,
#                                             w_r:w_r_value,
#                                             w_g:w_g_value,
#                                             w_b:w_b_value})
# plt.imshow(np.squeeze(output_value),cmap='gray')
# # plt.show()
#
# w_r_value = np.array([[-1,-2,-1], [0,0,0], [1,2,1]], dtype=float)
# w_g_value = np.array([[-1,-2,-1], [0,0,0], [1,2,1]], dtype=float)
# w_b_value = np.array([[-1,-2,-1], [0,0,0], [1,2,1]], dtype=float)
# w_value, output_value = sess.run(output, feed_dict={x:image_array,
#                                             w_r:w_r_value,
#                                             w_g:w_g_value,
#                                             w_b:w_b_value})
# w_r_value = np.array([[-1,0,1], [-2,0,2], [-1,0,1]], dtype=float)
# w_g_value = np.array([[-1,0,1], [-2,0,2], [-1,0,1]], dtype=float)
# w_b_value = np.array([[-1,0,1], [-2,0,2], [-1,0,1]], dtype=float)
# output_value2 = sess.run(output, feed_dict = {x:image_array,
#                                             w_r:w_r_value,
#                                             w_g:w_g_value,
#                                             w_b:w_b_value})
# plt.imshow(np.squeeze(output_value+output_value2),cmap='gray')
# plt.show()

import sys

x_mock = np.array(range(300))
x_mock = x_mock.reshape([10,10,3])

w_r_value = np.array([[1,0,0], [0,1,0], [0,0,1]], dtype=float)
w_g_value = np.array([[0,1,0], [0,1,0], [0,1,0]], dtype=float)
w_b_value = np.array([[0,0,0], [1,1,1], [0,0,0]], dtype=float)

kernel = np.stack([w_r_value, w_g_value, w_b_value], axis=2)
for depth in range(3):
    for height in range(10):
        for width in range(10):
            print(x_mock[height][width][depth], end='\t')
        print('')
    print('----------')

for depth in range(3):
    for height in range(3):
        for width in range(3):
            print(kernel[height][width][depth], end='\t')
        print('')
    print('----------')
output_numpy = np.zeros([8,8])
for height in range(10-2):
    for width in range(10-2):
        along_depth_sum = 0
        for depth in range(3):
            depth_result = (x_mock[height:height+3, width:width+3, depth] * kernel[depth]).sum()
            along_depth_sum += depth_result
        output_numpy[height][width] = along_depth_sum

for height in range(10-2):
    for width in range(10-2):
        print(output_numpy[height][width], end='\t')
    print('')